package com.meiyou.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.meiyou.mapper.MeiyouHongbaoMapper;
import com.meiyou.mapper.MeiyouUserMapper;
import com.meiyou.pojo.MeiyouHongbao;
import com.meiyou.service.HongBaoService;
import com.meiyou.utils.Msg;
@Service
public class HongBaoServiceImpl implements HongBaoService{
    @Resource
    private MeiyouHongbaoMapper meiyouHongbaoMapper;
    @Autowired
    MeiyouUserMapper userMapper;
    
	

	public int insHongBao(MeiyouHongbao meiyouHongbao) {
		return meiyouHongbaoMapper.insertHongBaoRetrunId(meiyouHongbao);
	}

	
	public Msg updHongBao(int id) {
		MeiyouHongbao hongbao =  meiyouHongbaoMapper.selectByPrimaryKey(id);
		if(hongbao!=null) {
			Map<String,Object> map = new HashMap<String,Object>();
			if(hongbao.gethState().equals("领取")) {
				Msg msg = Msg.success();
				map.put("state", "已领取");
				map.put("meijin", hongbao.gethMeijin());
			    msg.setExtend(map);
			    return msg;
			
			}else {
				if(userMapper.updAddMeijin(hongbao.gethMeijin(), hongbao.gethUBcount())>=1) {
					
						hongbao.sethState("领取");
						if(meiyouHongbaoMapper.updateByPrimaryKey(hongbao)>=1) {
							Msg msg = Msg.success();
							map.put("state", "领取成功");
							map.put("meijin", hongbao.gethMeijin());
						    msg.setExtend(map);
						    return msg;
						}
					
				}
				Msg msg = Msg.fail();
				msg.setMsg("数据操作异常");
				return msg;
			}
		}else {
			Msg msg = Msg.fail();
			msg.setMsg("未查询到红包");
			return msg;
		}
	}

}
